package pers.ljf.escloud.service.modules.app.impl;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import pers.ljf.escloud.common.dto.OSSClientInfo;
import pers.ljf.escloud.common.req.EntSaveRequest;
import pers.ljf.escloud.common.req.GetClientReq;
import pers.ljf.escloud.dao.entity.app.*;
import pers.ljf.escloud.dao.mapper.app.BucketMapper;
import pers.ljf.escloud.service.modules.app.*;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author laijinfan1
 * @since 2022-02-14
 */
@Service
public class BucketServiceImpl extends ServiceImpl<BucketMapper, Bucket> implements BucketService {

    @Resource
    private EnterpriseService enterpriseService;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private BucketDeptService bucketDeptService;

    @Override
    public void save(EntSaveRequest form) {
        Bucket bucket = new Bucket();
        bucket.setBucketName(form.getBucketName());
        bucket.setAccessKeyId(form.getAccessKeyId());
        bucket.setAccessKeySecret(form.getAccessKeySecret());
        bucket.setEndpoint(form.getEndpoint());

        Enterprise enterprise = enterpriseService.getOne(new QueryWrapper<Enterprise>().eq("name", form.getName()));
        Assert.isTrue(enterprise != null, "bucket对应企业不存在");
        bucket.setEnterpriseId(enterprise.getId());
        bucket.setType(1);
        save(bucket);

        Bucket bucketDB = getOne(new QueryWrapper<Bucket>().eq("access_key_id", form.getAccessKeyId()));
        Assert.isTrue(bucketDB != null, "bucket保存未成功");
        BucketDept bucketDept = new BucketDept();
        bucketDept.setBucketId(bucketDB.getId());
        Department deptDB = departmentService.getOne(new QueryWrapper<Department>().eq("enterprise_name", form.getName()).eq("parent_id", 0L));
        Assert.isTrue(deptDB != null, "找不到新建的部门");
        bucketDept.setDeptId(deptDB.getId());
        bucketDeptService.save(bucketDept);
    }

    @Resource
    UserService userService;
    @Override
    //暂时仅考虑一个部门一个bucket
    public OSSClientInfo getClient(GetClientReq req) {
        Long deptId = null;
        if (req.getUserId() != null) {
            User user = userService.getById(req.getUserId());
            deptId = user.getDeptId();
        } else if (req.getDeptId() != null) {
            deptId = req.getDeptId();
        } else {
            deptId = departmentService.getOne(new QueryWrapper<Department>().eq("name", req.getEntName()).eq("parent_id", 0)).getId();
        }
        Assert.isTrue(deptId != null,"获取client请求未指定user或dept");
        BucketDept bucketDept = bucketDeptService.getOne(new QueryWrapper<BucketDept>().eq("dept_id", deptId));
        Long bucketId = bucketDept.getBucketId();
        Bucket bucket = getById(bucketId);
        OSSClient client = (OSSClient) new OSSClientBuilder().build(bucket.getEndpoint(), bucket.getAccessKeyId(), bucket.getAccessKeySecret());
        return OSSClientInfo.builder().client(client).bucketName(bucket.getBucketName()).build();
    }
}
